

<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title" style="margin-left: 120px;margin-bottom: 20px">用户登录</h1>
        <el-form
            :model="loginForm"
            label-width="100px"
            :rules="rules"
            ref="loginForm"
        >
          <el-form-item label="账号" prop="no">
            <el-input
                style="width: 200px"
                type="text"
                v-model="loginForm.no"
                autocomplete="off"
                size="small"
            ></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
                style="width: 200px"
                type="password"
                v-model="loginForm.password"
                show-password
                autocomplete="off"
                size="small"
                @keyup.enter.native="confirm"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                @click="confirm"
                :disabled="confirm_disabled"
            >
              确定
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        no: '',
        password: ''
      },
      rules: {
        no: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    confirm() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          this. $axios.post(this.$httpUrl + '/user/login', this.loginForm).then(res => res.data).then(res => {
            console.log(res)
            if (res.code == 200) {
              //存储
              sessionStorage.setItem('CurUser', JSON.stringify(res.data))
              //跳转到主页
              this.$router.replace('/MyIndex');
            } else {
              this.confirm_disabled = false;
              alert('账号或密码错误');
              return false;
            }
          });

        } else {
          this.confirm_disabled = false;
          console.log('检验失败');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #eaf0f8;
}
.loginDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;  /* 修正：原错误的 margin-twjl */
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: #eaf0f8;
  border-radius: 2%;

}
.login-content{
  width: 400px;
 height: 300px;
  position: absolute;
  top: 25px;
  left: 25px;
}
</style>
